package com.example.seabedmusic.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.SettingsAdapter;
import com.example.seabedmusic.entity.SettingItem;
import java.util.ArrayList;
import java.util.List;

public class AboutActivity extends AppCompatActivity implements SettingsAdapter.OnSettingItemClickListener {

    private RecyclerView rvAbout;
    private SettingsAdapter adapter;
    private TextView tvVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        initViews();
        setupAboutList();
        setupVersionInfo();
    }

    private void initViews() {
        rvAbout = findViewById(R.id.rvAbout);
        tvVersion = findViewById(R.id.tvVersion);
        rvAbout.setLayoutManager(new LinearLayoutManager(this));
        setTitle("关于");
    }

    private void setupVersionInfo() {
        try {
            String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            tvVersion.setText("版本号: v" + versionName);
        } catch (Exception e) {
            tvVersion.setText("版本号: v1.0.0");
        }
    }

    private void setupAboutList() {
        List<SettingItem> items = new ArrayList<>();

        items.add(new SettingItem("用户协议", R.drawable.ic_document, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("隐私政策", R.drawable.ic_privacy, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("开源许可", R.drawable.ic_opensource, SettingItem.TYPE_NORMAL));
        items.add(new SettingItem("检查更新", R.drawable.ic_update, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("意见反馈", R.drawable.ic_feedback, SettingItem.TYPE_NORMAL));
//        items.add(new SettingItem("给我们评分", R.drawable.ic_star, SettingItem.TYPE_NORMAL));

        adapter = new SettingsAdapter(items);
        adapter.setOnSettingItemClickListener(this);
        rvAbout.setAdapter(adapter);
    }

    @Override
    public void onSettingItemClick(SettingItem item) {
        switch (item.getTitle()) {
            case "用户协议":
                openWebPage("https://example.com/user-agreement");
                break;
            case "隐私政策":
                openWebPage("https://example.com/privacy-policy");
                break;
            case "开源许可":
                showOpenSourceLicenses();
                break;
            case "检查更新":
                checkForUpdates();
                break;
            case "意见反馈":
                sendFeedback();
                break;
            case "给我们评分":
                rateApp();
                break;
        }
    }

    private void openWebPage(String url) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
        } catch (Exception e) {
            android.widget.Toast.makeText(this, "无法打开链接", android.widget.Toast.LENGTH_SHORT).show();
        }
    }

    private void showOpenSourceLicenses() {
        new android.app.AlertDialog.Builder(this)
                .setTitle("开源许可")
                .setMessage("本应用使用了以下开源项目:\n\n• Glide - 图片加载库\n• Gson - JSON解析库\n• Material Design - UI组件")
                .setPositiveButton("确定", null)
                .show();
    }

    private void checkForUpdates() {
        android.widget.Toast.makeText(this, "已是最新版本", android.widget.Toast.LENGTH_SHORT).show();
    }

    private void sendFeedback() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:feedback@seabedmusic.com"));
        intent.putExtra(Intent.EXTRA_SUBJECT, "海底音乐反馈意见");
        try {
            startActivity(Intent.createChooser(intent, "发送邮件"));
        } catch (Exception e) {
            android.widget.Toast.makeText(this, "未找到邮件应用", android.widget.Toast.LENGTH_SHORT).show();
        }
    }

    private void rateApp() {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName()));
            startActivity(intent);
        } catch (Exception e) {
            android.widget.Toast.makeText(this, "无法打开应用市场", android.widget.Toast.LENGTH_SHORT).show();
        }
    }
}